<template>
  <div class="lindi" v-if="ResultData.length">
    <div class="lindi_top">
      <div class="lindi_top_text">
        林地
      </div>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        border
        height="340"
        style="width: 100%; margin-top: 10px;"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          prop="name"
          min-width="60px"
          align="center"
          label="类别"
        >
        </el-table-column>
        <el-table-column
          prop="ha"
          align="center"
          :formatter="rounding2"
          min-width="40px"
          label="面积(ha)"
        >
        </el-table-column>
        <el-table-column
          prop="mu"
          align="center"
          :formatter="rounding"
          min-width="45px"
          label="面积(亩)"
        >
        </el-table-column>
        <el-table-column
          prop="ration"
          align="center"
          min-width="30px"
          label="占比(%)"
        >
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      tableData: [],
      tableDatas1: [
        {
          id: "公益林",
          name: "52.25",
          amount1: "234",
        },
        {
          id: "其中:重点公益林",
          name: "54.25",
          amount1: "234",
        },
        {
          id: "一般公益林",
          name: "250.1",
          amount1: "234",
        },
        {
          id: "商品林",
          name: "21.01",
          amount1: "234",
        },
        {
          id: "其中:重点商品林",
          name: "45.2",
          amount1: "234",
        },
        {
          id: "一般商品林",
          name: "56.2",
          amount1: "234",
        },
        {
          id: "合计",
          name: "56.2",
          amount1: "234",
        },
      ],
    };
  },
  methods: {
    rounding(row, colum) {
      return parseFloat(row[colum.property]).toFixed(2);
    },
    rounding1(row, colum) {
      return parseFloat(row[colum.property]).toFixed(1);
    },
    rounding2(row, colum) {
      return parseFloat(row[colum.property]).toFixed(3);
    },
    getresultdata() {
      console.log(this.ResultData, "this.ResultData");

      let num = 0;
      this.ResultData.forEach((item, index) => {
        if (index < this.ResultData.length - 1) {
          num += Number(item.ration);
        } else {
          item.ration = Number((100 - num).toFixed(1));
        }
      });


      let newdata = {};
      this.ResultData.forEach((item, index) => {
        if (!newdata[item.category]) {
          newdata[item.category] = {
            name: item.category,
            ha: item.ha,
            mu: item.mu,
            m2: item.m2,
            ration: item.ration,
            id: index + item.category,
            children: [
              {
                name: item.subCategory,
                id: index,
                ha: item.ha,
                m2: item.m2,
                mu: item.mu,
                ration: item.ration,
              },
            ],
          };
        } else {
          newdata[item.category].ha += item.ha;
          newdata[item.category].mu += item.mu;
          newdata[item.category].m2 += item.m2;
          newdata[item.category].ration += item.ration;
          newdata[item.category].children.push({
            name: item.subCategory,

            id: index,
            ha: item.ha,
            m2: item.m2,
            mu: item.mu,
            ration: item.ration,
          });
        }
      });
      console.log(newdata, "newdata");
      let dataarr = [];
      let sum = {
        id: "sum",
        mu: 0,
        ha: 0,
        ration: 0,
        name: "总计",
      };
      for (const i in newdata) {
        console.log(newdata[i], "iiii");
        dataarr.push(newdata[i]);
        sum.mu += Number(newdata[i].mu.toFixed(2));
        sum.ha += Number(newdata[i].ha.toFixed(3));
        sum.ration += Number(newdata[i].ration.toFixed(0));
      }
      dataarr.push(sum);

      // console.log(...newdata,'newdata');
      dataarr.forEach((item) => {
        console.log(item, "dataarr的值");
        if (item.name == "总计") {
          item.ration = item.ration.toFixed(0);
        } else {
          item.ration = item.ration.toFixed(1);
        }
        if (Number(item.ration) > 100) {
          // 当占比超过100时，都设置为100
          item.ration = 100;
        }
      });
      this.tableData = dataarr;
      console.log(dataarr, "dataarrdataarrdataarr");
    },
  },
  computed: {
    ...mapState("Sjsx", ["ResultData"]),
  },
  watch: {},
  mounted() {
    this.getresultdata();
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table,
::v-deep .el-table__expanded-cell {
  background-color: rgba(14, 85, 238, 0.109) !important;
  border: 1px solid #466393cc;
}
::v-deep .el-table__body-wrapper {
  height: 270px !important;
}
// 滚动条的宽度
::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}
// 滚动条的滑块
::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background: linear-gradient(
    180deg,
    rgba(131, 166, 166, 0.1),
    rgba(131, 166, 166, 0.8),
    rgba(131, 166, 166, 0.1)
  );
}
// 滚动条里面的轨道(即背景颜色)
::v-deep .el-table__body-wrapper::-webkit-scrollbar-track {
  // background-color: #5b739b2b;
  background-color: rgba(41, 71, 89, 0.4);
}
::v-deep .el-table__header th,
::v-deep .el-table__header tr {
  // background-color: #17B3A3;
  color: #fff;
}
::v-deep .el-table th,
::v-deep .el-table tr,
::v-deep .el-table td {
  background-color: rgba(251, 251, 251, 0.109) !important;

  border: 0px solid #466393cc;
}
::v-deep .el-table__expand-icon {
  color: #fff;
}
/* 去掉中间数据的分割线 */
::v-deep .el-table__row > td {
  // border: 1px solid #466393cc;
}
/* 去掉上面的线 */
::v-deep .el-table th.is-leaf {
  // border: 1px solid #466393cc;
}
/* 去掉最下面的那一条线 */
::v-deep .el-table::before {
  height: 0px;
}
::v-deep .el-table__body tr {
  background-color: transparent !important;

  border: 1px solid #466393cc;
}
// .el-table__body tr:hover > td {
//     background-color: transparent !important;

//     border: 0px solid #134087;
// }
::v-deep .el-table td.el-table__cell,
::v-deep .el-table th.el-table__cell.is-leaf {
  border-bottom: 1px solid #466393cc;
  border-left: 1px solid #466393cc;
  font-size: 18px;
}
::v-deep .el-table--border th.el-table__cell.gutter:last-of-type {
  border-bottom: 1px solid #466393cc;
}
::v-deep .el-table--border .el-table__cell,
::v-deep
  .el-table__body-wrapper
  .el-table--border.is-scrolling-left
  ~ .el-table__fixed {
  border-right: 0px solid #ebeef5;
  color: #fff;
}
::v-deep .el-table--border::after,
::v-deep .el-table--group::after,
::v-deep .el-table::before {
  content: "";
  position: absolute;
  background-color: #466393cc;
  z-index: 1;
}

::v-deep .el-table--scrollable-y .el-table__body-wrapper {
  &::-webkit-scrollbar {
    width: 4px;
  }
  &::-webkit-scrollbar-thumb {
    background: linear-gradient(
      180deg,
      rgba(131, 166, 166, 0.1),
      rgba(131, 166, 166, 0.8),
      rgba(131, 166, 166, 0.1)
    );
    border-radius: 10px;
  }
  &::-webkit-scrollbar-track-piece {
    background: transparent;
  }
}
::v-deep .el-table th.el-table__cell > .cell {
  padding-left: 5px;
  padding-right: 5px;
}
.lindi {
  margin-bottom: 35px;
  .lindi_top {
    background: url("../../../../../assets/images/home/top.png");
    background-size: cover;
    margin-top: 10px;
    .lindi_top_text {
      padding-left: 16px;
      font-family: OPPOSans;
      font-style: normal;
      font-weight: normal;
      font-size: 20px;
      line-height: 24px;
      color: #ffffff;
    }
  }
}
</style>
